package com.hyacinth.forumbite.controller;

import com.hyacinth.forumbite.common.AppResult;
import com.hyacinth.forumbite.common.ResultCode;
import com.hyacinth.forumbite.exception.ApplicationException;
import com.hyacinth.forumbite.model.Board;
import com.hyacinth.forumbite.service.BoardService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Yun
 * @Date: 2025/5/21
 */
@Slf4j
@RequestMapping("/board")
@RestController
@Tag(name = "板块相关接口")
public class BoardController {
    @Resource
    private BoardService boardService;
    @Value("${bit-forum.index.board-num:9}")
    private Integer num;

    @Operation(summary = "板块列表")
    @GetMapping("/topList")
    public AppResult<List<Board>> getBoardList() {
        log.info("板块数量: " + num);
        List<Board> boards = boardService.selectByNum(num);
        if (boards == null) {
            boards = new ArrayList<>();
        }
        return AppResult.success(boards);
    }

    @Operation(summary = "获取板块信息")
    @Parameters({
            @Parameter(name = "id", description = "板块id")
    })
    @GetMapping("/info")
    public AppResult<Board> getBoard(@RequestParam(value = "id") @NonNull Long id) {
        Board board = boardService.selectById(id);
        if (board == null || board.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        return AppResult.success(board);
    }
}
